$("#list").jqGrid({
   	url:'/agent/agentList/?q=2',
	datatype: "json",
   	colNames:['Agent Id','Agent Name', 'Alias'],
   	colModel:[
   		{name:'agentid',index:'agentid', width:55,editable:false,editoptions:{readonly:true,size:10}},
   		{name:'agentname',index:'agentname', width:200,editable:true,editrules:{required:true},editoptions:{size:10}},
   		{name:'alias',index:'alias', width:200,editable:true,editrules:{required:true},editoptions:{size:10}}
   	],
   	rowNum:10,
   	rowList:[10,20,30],
   	pager: '#pager',
   	toppager:true,
    cloneToTop:true,
   	sortname: 'agentname',
    viewrecords: true,
    sortorder: "asc",
    editurl: '/agent/maniplulate/',
    caption:"Agent Maintenance"
    /*
    gridComplete: function() {
        var grid = jQuery("#agentList");
        var ids = grid.jqGrid('getDataIDs');
        for (var i = 0; i < ids.length; i++) {
            var rowId = ids[i];
            var assign = "<input style='height:22px;width:75px;' " +
                           "type='button' value='Check Out' " +
                           "onclick=\"assign('#agentList'," +
                           rowId + ");\" />";
            grid.jqGrid('setRowData', rowId, { action: assign });
        }
    }*/
});
jQuery("#agentList").jqGrid('navGrid','#pager',
{search:true,cloneToTop:true}, //options
{height:120,reloadAfterSubmit:true,beforeSubmit:validateData,closeAfterAdd: true,closeAfterEdit: true}, // edit options
{height:120,reloadAfterSubmit:true,beforeSubmit:validateData,closeAfterAdd: true,closeAfterEdit: true}, // add options
{reloadAfterSubmit:false}, // del options
{} // search options
);
$("#agentList").setGridHeight(230,true);

function assign(gridId,rowId) {
	var grid = $("#agentList");
	var sel_id = grid.jqGrid('getGridParam', 'selrow');
	var myCellData = grid.jqGrid('getCell', sel_id, 'agentid');
	if(myCellData) {
		alert(myCellData);
	} else {
		alert('No selected row');
	}
}


//var issuccessMessage;
function validateData(postdata, formid) {
	var message;
	var issuccess;
	function set_message(newissuccess,newmessage) {
		issuccess = newissuccess;
		message = newmessage;
	}
	$.ajax({
     url: '/agent/validateAgentInfo',
     type: "POST",
     async: false, // set to false so order of operations is correct
     data: postdata,
     success: function(data){
		if(data) set_message(false,data);
		//else set_message(true,'Data successfully saved!');
	}});
	if(message)	return [false,message,"1"];
	else return [true,"","1"];;
}
